using MaterialSkin.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
using VideoAutoGenerator.Core;

namespace VideoAutoGenerator.UI.UserControls
{
    public partial class WaveformSettingsControl : MaterialSkin.Controls.MaterialCard
    {
        // 波形显示设置控件
        private MaterialLabel enabledLabel;
        private MaterialCheckbox enabledCheckBox;
        
        private MaterialLabel heightLabel;
        private NumericUpDown heightNumeric;
        
        private MaterialLabel colorLabel;
        private MaterialTextBox colorTextBox;
        private MaterialButton colorButton;
        
        private MaterialLabel positionLabel;
        private ComboBox positionComboBox;
        
        public WaveformSettingsControl()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent()
        {
            this.SuspendLayout();
            
            // 创建控件
            CreateControls();
            
            // 设置控件位置和样式
            SetupLayout();
            
            this.ResumeLayout(false);
        }
        
        private void CreateControls()
        {
            // 启用开关
            enabledLabel = new MaterialLabel();
            enabledCheckBox = new MaterialCheckbox();
            
            // 高度
            heightLabel = new MaterialLabel();
            heightNumeric = new NumericUpDown();
            
            // 颜色
            colorLabel = new MaterialLabel();
            colorTextBox = new MaterialTextBox();
            colorButton = new MaterialButton();
            
            // 位置
            positionLabel = new MaterialLabel();
            positionComboBox = new ComboBox();
        }
        
        private void SetupLayout()
        {
            // 设置控件属性
            this.AutoScroll = true;
            this.Dock = DockStyle.Fill;
            
            // 启用开关
            enabledLabel.Text = "启用波形显示:";
            enabledCheckBox.Checked = true;
            
            // 高度
            heightLabel.Text = "波形高度(像素):";
            heightNumeric.Minimum = 20;
            heightNumeric.Maximum = 500;
            heightNumeric.Value = 100;
            heightNumeric.DecimalPlaces = 0;
            
            // 颜色
            colorLabel.Text = "波形颜色:";
            colorTextBox.Width = 400; // 设置固定宽度为400
            colorButton.Text = "选择";
            colorButton.Click += ColorButton_Click; // 添加颜色选择按钮的点击事件
            
            // 位置
            positionLabel.Text = "波形位置:";
            positionComboBox.Items.Add("top");
            positionComboBox.Items.Add("bottom");
            positionComboBox.Items.Add("left");
            positionComboBox.Items.Add("right");
            positionComboBox.SelectedIndex = 1; // 默认底部
            
            // 设置控件位置
            var layoutPanel = new MaterialSkin.Controls.MaterialLabel();
            layoutPanel.Dock = DockStyle.Fill;
            
            var tableLayout = new TableLayoutPanel();
            tableLayout.Dock = DockStyle.Fill;
            tableLayout.ColumnCount = 3;
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 120));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 80));
            tableLayout.RowCount = 5;
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            tableLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 50));
            
            // 添加控件到表格布局
            tableLayout.Controls.Add(enabledLabel, 0, 0);
            tableLayout.Controls.Add(enabledCheckBox, 1, 0);
            
            tableLayout.Controls.Add(heightLabel, 0, 1);
            tableLayout.Controls.Add(heightNumeric, 1, 1);
            tableLayout.SetColumnSpan(heightNumeric, 2);
            
            tableLayout.Controls.Add(colorLabel, 0, 2);
            tableLayout.Controls.Add(colorTextBox, 1, 2);
            tableLayout.Controls.Add(colorButton, 2, 2);
            
            tableLayout.Controls.Add(positionLabel, 0, 3);
            tableLayout.Controls.Add(positionComboBox, 1, 3);
            tableLayout.SetColumnSpan(positionComboBox, 2);
            
            layoutPanel.Controls.Add(tableLayout);
            for (int i = 0; i < tableLayout.Controls.Count; i++)
            {
                var ctrl = tableLayout.Controls[i];
                if (ctrl is MaterialTextBox textBox)
                {
                    textBox.Width = 400;
                    // 设置控件在容器中的对齐方式
                    textBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
                else if (ctrl is NumericUpDown numericUpDown)
                {
                    // 设置NumericUpDown控件的锚定属性
                    numericUpDown.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
                else if (ctrl is ComboBox comboBox)
                {
                    // 设置ComboBox控件的锚定属性
                    comboBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
            }
            this.Controls.Add(layoutPanel);
        }
        
        // 颜色选择按钮点击事件处理程序
        private void ColorButton_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                // 将颜色转换为RGB十六进制格式（无#前缀）
                colorTextBox.Text = colorDialog.Color.R.ToString("X2") + 
                                   colorDialog.Color.G.ToString("X2") + 
                                   colorDialog.Color.B.ToString("X2");
            }
        }
        
        // 从配置管理器加载配置数据到控件
        public void LoadConfig()
        {
            var config = ConfigManager.Instance;
            
            enabledCheckBox.Checked = config.GetBool("waveform.enabled", true);
            heightNumeric.Value = config.GetInt("waveform.height", 100);
            colorTextBox.Text = config.GetString("waveform.color", "00ff00");
            var position = config.GetString("waveform.position", "bottom");
            
            // 设置位置下拉框的选中项
            if (positionComboBox.Items.Contains(position))
            {
                positionComboBox.SelectedItem = position;
            }
            else
            {
                positionComboBox.SelectedIndex = 1; // 默认底部
            }
        }
        
        // 从控件保存配置数据到配置管理器
        public void SaveConfig()
        {
            var config = ConfigManager.Instance;
            
            config.SetValue("waveform.enabled", enabledCheckBox.Checked);
            config.SetValue("waveform.height", (int)heightNumeric.Value);
            config.SetValue("waveform.color", colorTextBox.Text);
            
            if (positionComboBox.SelectedItem != null)
            {
                config.SetValue("waveform.position", positionComboBox.SelectedItem.ToString());
            }
            
            config.SaveConfig();
        }
    }
}